package com.shd.edanpei.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.ScoreModel;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.ui.adapter.AccountAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.contract.AccountContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.UrlEnum;
import com.shd.edanpei.presenter.AccountPresenter;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的账户
 */
public class AccountActivity extends BaseActivity<AccountPresenter> implements AccountContract.AccountIView {

    /**
     * 收支类型
     * IN：收入
     * OUT：支出
     */
    public static final String IN = "in";
    public static final String OUT = "out";

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_headline_right)
    TextView tvHeadlineRight;
    @BindView(R.id.tv_scoreAmount)
    TextView tvScoreAmount;
    @BindView(R.id.rg_score)
    RadioGroup radioGroup;
    @BindView(R.id.rlv_score)
    RecyclerView recyclerView;
    @BindView(R.id.rl_1)
    View view1;
    @BindView(R.id.rl_2)
    View view2;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.tv_score2)
    TextView tvScore2;
    @BindView(R.id.image)
    ImageView imageView;

    private AccountAdapter mAdapter;
    private String mAccountType = IN;

    @Override
    protected int setLayout() {
        return R.layout.activity_account;
    }

    @Override
    protected AccountPresenter bindPresenter() {
        return new AccountPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        //标题 & 右标题
        headline.setText(getString(R.string.account_title));
        tvHeadlineRight.setText(getString(R.string.account_rule));
        //adapter
        mAdapter = new AccountAdapter(getContext(), mAccountType);
        //recyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

        mPresenter.getAccountInfo();
        mPresenter.refresh(mAccountType);

        int roleType = DaoManager.getInstance().getUserBean().getRoleType();
        if (roleType == RoleEnum.bank.getId()) {
            view1.setVisibility(View.VISIBLE);
        } else {
            view2.setVisibility(View.VISIBLE);
        }

        mPresenter.getImage();
    }

    @Override
    protected void initListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mAccountType = checkedId == R.id.rb_in ? IN : OUT;
                mAdapter.setType(mAccountType);
                mPresenter.refresh(mAccountType);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore(mAccountType);
            }
        }, recyclerView);
    }

    /**
     * 点击返回
     */
    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    /**
     * 点击积分规则
     */
    @OnClick(R.id.tv_headline_right)
    void onRule() {
        WebActivity.start(getContext(), getString(R.string.account_rule), null, UrlEnum.b.getType());
    }

    @OnClick(R.id.image)
    void to() {
        RxBus.get().send(RxBusCode.To_PublishFragment);
        finish();
    }


    /**
     * 点击提现按钮
     */
    @OnClick(R.id.tv_score_withdraw)
    void onWithdraw() {
        //是否实名认证
        if (DaoManager.getInstance().getUserBean().getIsReal()) {
            //已认证
            startActivity(WithdrawActivity.class);
        } else {
            //未实名认证
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(getString(R.string.unauthenticated))
                    .setMessage(getString(R.string.unauthenticated_hint))
                    .setNegativeButton(getString(R.string.cancel), null)
                    .setPositiveButton(getString(R.string.to_authentication), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Bundle bundle = new Bundle();
                            bundle.putInt(Constant.FROM, CertificationActivity.FORM_DATA);
                            startActivity(CertificationActivity.class, bundle);
                        }
                    }).show();

        }
    }

    @OnClick(R.id.tv_score2)
    void onHelp() {
        WebActivity.start(this, null, null, UrlEnum.k.getType());
    }

    @Override
    public void setAccountInfo(ScoreModel data) {
        tvScoreAmount.setText(data.getPointsStr());
        tvTotal.setText("¥" + data.getAmount());
        tvScore2.setText("(" + data.getPoints() + "积分)");
    }

    @Override
    public AccountAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public void setImage(String values) {
        Glide.with(this).load(values).into(imageView);
        imageView.setVisibility(View.VISIBLE);
    }


}
